unit DatamoduleGrid;

interface

uses
  SysUtils, Classes, ImgList, Controls, Menus, Forms, DB,
  cxGridCustomView, cxGridDBTableView, cxGridDBBandedTableView, cxGrid;

type
  TdmBaseGrid = class(TDataModule)
    ilActions: TImageList;
    pmLight: TPopupMenu;
    miFullExpand: TMenuItem;
    miFullCollapse: TMenuItem;
    N12: TMenuItem;
    miExport2Excel: TMenuItem;
    miRefresh: TMenuItem;
    procedure miFullExpandClick(Sender: TObject);
    procedure miFullCollapseClick(Sender: TObject);
    procedure miExport2ExcelClick(Sender: TObject);
    procedure miRefreshClick(Sender: TObject);
  private
    { Private declarations }
    function GetGridView(AComponent: TComponent): TcxCustomGridView;
    function GetGrid(AComponent: TComponent): TcxGrid;
    function GetDataset(AComponent: TComponent): TDataset;
  public
    { Public declarations }
  end;

var
  dmBaseGrid: TdmBaseGrid;

implementation

uses cxUtils;
{$R *.dfm}

function TdmBaseGrid.GetGridView(AComponent: TComponent): TcxCustomGridView;
begin
  if pmLight.PopupComponent is TcxGridSite then
  begin
    Result := TcxGridSite(AComponent).GridView;
  end
  else
    raise Exception.Create('The pmLight.PopupComponent is not TcxGridSite.');
end;

function TdmBaseGrid.GetDataset(AComponent: TComponent): TDataset;
begin
  //Result := GetGridView(AComponent).DataController.DataSource.DataSet;
  Result := nil;
end;

function TdmBaseGrid.GetGrid(AComponent: TComponent): TcxGrid;
begin
  Result := TcxGrid(GetGridView(AComponent).GetParentComponent);
end;

procedure TdmBaseGrid.miExport2ExcelClick(Sender: TObject);
begin
  cxGridExport(GetGrid(pmLight.PopupComponent), 'xls');
end;

procedure TdmBaseGrid.miFullCollapseClick(Sender: TObject);
var
  GridView: TcxCustomGridView;
begin
  GridView := GetGridView(pmLight.PopupComponent);

  if (GridView is TcxGridDBTableView) or (GridView is TcxGridDBBandedTableView)
    then
    GridView.DataController.Groups.FullCollapse;
end;

procedure TdmBaseGrid.miFullExpandClick(Sender: TObject);
var
  GridView: TcxCustomGridView;
begin
  GridView := GetGridView(pmLight.PopupComponent);

  if (GridView is TcxGridDBTableView) or (GridView is TcxGridDBBandedTableView)
    then
    GridView.DataController.Groups.FullExpand;

  // ds := TcxGridDBTableView(GridView).DataController.DataSource.DataSet;
  // ds.Refresh;

  {
    if (TcxGridTableView(db_GridM.ActiveView).ViewData.RecordCount <> 0) and
    (TcxGridTableView(db_GridM.ActiveView).ViewData.Records[0] is TcxGridGroupRow) then
    pRecFirstExpanded := TcxGridGroupRow(TcxGridTableView(db_GridM.ActiveView).ViewData.Records[0]).Expanded;
    }
end;

procedure TdmBaseGrid.miRefreshClick(Sender: TObject);
var
  ds: TDataset;
  bm: TBookmark;
begin
  Screen.Cursor := crHourGlass;
  try
    ds := GetDataset(pmLight.PopupComponent);
    bm := ds.GetBookmark;
    try
      ds.Close;
    finally
      ds.Open;
      if ds.BookmarkValid(bm) then
        ds.GotoBookmark(bm);

      ds.FreeBookmark(bm);
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

end.
